﻿using ETask.Api.Data;
using ETask.Api.Mappers;
using ETask.Api.Models;
using ETask.Api.Repositories;

namespace ETask.Api.Services
{
    /// <summary>
    /// 通用服务基类，提供基础的CRUD操作
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    /// <typeparam name="TDto">DTO类型</typeparam>
    /// <typeparam name="TCreateDto">创建DTO类型</typeparam>
    /// <typeparam name="TUpdateDto">更新DTO类型</typeparam>
    public class GenericService<TEntity, TDto, TCreateDto, TUpdateDto> : IGenericService<TEntity, TDto, TCreateDto, TUpdateDto>
        where TEntity : BaseEntity, new()
        where TDto : class, new()
        where TCreateDto : class, new()
        where TUpdateDto : class, new()
    {
        protected readonly IRepository<TEntity> _repository;
        protected readonly GenericMapper<TEntity, TDto, TCreateDto, TUpdateDto> _mapper;

        public GenericService(IRepository<TEntity> repository,
                             GenericMapper<TEntity, TDto, TCreateDto, TUpdateDto> mapper)
        {
            _repository = repository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        public async Task<PagedResult<TDto>> GetPagedAsync(int pageNumber, int pageSize)
        {
            var pagedEntities = await _repository.GetPagedAsync(new PaginationParameters
            {
                PageNumber = pageNumber,
                PageSize = pageSize
            });

            return new PagedResult<TDto>
            {
                Items = _mapper.ToDtoList(pagedEntities.Items),
                TotalCount = pagedEntities.TotalCount,
                CurrentPage = pageNumber,
                PageSize = pageSize,
                TotalPages = pagedEntities.TotalPages
            };
        }

        /// <summary>
        /// 根据ID获取单个实体
        /// </summary>
        public async Task<TDto?> GetByIdAsync(int id)
        {
            var entity = await _repository.GetByIdAsync(id);
            return entity != null ? _mapper.ToDto(entity) : null;
        }

        /// <summary>
        /// 创建新实体
        /// </summary>
        public async Task<TDto> CreateAsync(TCreateDto createDto)
        {
            var entity = _mapper.ToEntity(createDto);
            entity.CreatedAt = DateTime.Now;
            LoginUser login = UserContext.CurrentUser;
            if (login !=null)
            {
                entity.CreateBy = login.Id;
            }

            await _repository.AddAsync(entity);
            return _mapper.ToDto(entity);
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        public async Task<bool> UpdateAsync(int id, TUpdateDto updateDto)
        {
            var entity = await _repository.GetByIdAsync(id);
            if (entity == null)
                return false;

            _mapper.UpdateEntityFromDto(updateDto, entity);
            entity.UpdatedAt = DateTime.Now;
            LoginUser login = UserContext.CurrentUser;
            if (login != null)
            {
                entity.UpdateBy = login.Id;
            }
            await _repository.UpdateAsync(entity);
            return true;
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        public async Task<bool> DeleteAsync(int id)
        {
            await _repository.DeleteAsync(id);
            return true;
        }
    }
}
